package com.wenbo.wenbo.service.impl;

import com.wenbo.wenbo.service.FishService;
import com.wenbo.wenbo.service.MsgService;
import com.wenbo.wenbo.service.WeChatService;
import com.wenbo.wenbo.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ywb
 * @date 2021年12月08日 10:03
 */
@Service
public class FishServiceImpl implements FishService {

    private static final Logger log = LoggerFactory.getLogger(FishServiceImpl.class);
    private int week = 0;
    private int plusday = 0;


    @Value("${children.birth}")
    private String birth;
    @Value("${children.pregnancy}")
    private String pregnancy;


    @Resource
    private MsgService msgService;
    @Resource
    private WeChatService weChatService;

    @Override
    public void childrenInfo() {

        String phone = "15223067069";
        //String phone="15696391732";
        Date now = DateUtil.getNow();
        String day = howDay(now);
        String big = howBig(now);
        String heavy = howHeavy(this.week, this.plusday);
//        System.out.println("亲爱的老婆！你已经辛苦孕育孩子：" + big + "，他出生还有" + day + "天，他现在有" + heavy + "克重了，爱你(✿◠‿◠)。");
//        log.info("亲爱的老婆！你已经辛苦孕育孩子：" + big + "，他出生还有" + day + "天，他现在有" + heavy + "克重了，爱你(✿◠‿◠)。");
        String[] params = new String[]{big, day, heavy};
        //msgService.sendMsg(phone, params);
        String accessToken=weChatService.getAccessToken();
        log.info(big);
        log.info(heavy);
        log.info(day);
        weChatService.sendMessage(accessToken,big,heavy,day);

    }

    //获取还有多少天出生
    private String howDay(Date now) {
        //获取预产期
        Date birthDay = DateUtil.valueOf(birth);
        return String.valueOf(DateUtil.daysBetween(now, birthDay))+"天";//计算还有好多天出生
    }

    //获取有多少周了
    private String howBig(Date now) {
        //获取怀孕时间
        Date pregnancyTime = DateUtil.valueOf(pregnancy);
        int day = DateUtil.daysBetween(pregnancyTime, now);//计算怀孕了多久
        int week = day / 7;
        int day2 = day % 7;
        String big = week + "周+" + day2 + "天";
        this.week = week;
        this.plusday = day2;
        return big;
    }

    private String howHeavy(int week, int day) {
        List<Double> heavy = getHeavy();
        Double base = heavy.get(week - 1);
        if (week == 40) {
            return base + "克";
        }
        Double next = heavy.get(week);
        base = base + (next - base) / 7 * day;
        BigDecimal b = new BigDecimal(base);
        return b.setScale(0, BigDecimal.ROUND_DOWN).toString()+"克";
    }


    private List<Double> getHeavy() {
        List<Double> heavy = new ArrayList<>();
        heavy.add(0.1d);
        heavy.add(0.1d);
        heavy.add(0.1d);
        heavy.add(0.1d);
        heavy.add(0.3d);
        heavy.add(0.6d);
        heavy.add(1.2d);
        heavy.add(1.5d);
        heavy.add(3d);
        heavy.add(5d);

        heavy.add(10d);
        heavy.add(17d);
        heavy.add(30d);
        heavy.add(50d);
        heavy.add(82d);
        heavy.add(113d);
        heavy.add(161d);
        heavy.add(210d);
        heavy.add(270d);
        heavy.add(325d);

        heavy.add(400d);
        heavy.add(450d);
        heavy.add(550d);
        heavy.add(620d);
        heavy.add(700d);
        heavy.add(760d);
        heavy.add(835d);
        heavy.add(980d);
        heavy.add(1150d);
        heavy.add(1300d);

        heavy.add(1500d);
        heavy.add(1700d);
        heavy.add(2000d);
        heavy.add(2225d);
        heavy.add(2400d);
        heavy.add(2600d);
        heavy.add(2750d);
        heavy.add(3000d);
        heavy.add(3300d);
        heavy.add(3500d);
        return heavy;

    }
}
